namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using DotNetNuke.Services.Localization;
    using System;
    using System.Collections;
    using System.Web.UI.HtmlControls;

    public class PaLogger : ResourceInstallerBase
    {
        private string _ErrorClass;
        private string _HighlightClass;
        private string _NormalClass;
        private ArrayList m_Logs = new ArrayList();
        private bool m_Valid = true;

        public void Add(Exception ex)
        {
            this.AddFailure(base.EXCEPTION + ex.ToString());
        }

        public void AddFailure(string failure)
        {
            this.m_Logs.Add(new PaLogEntry(PaLogType.Failure, failure));
            this.m_Valid = false;
        }

        public void AddInfo(string info)
        {
            this.m_Logs.Add(new PaLogEntry(PaLogType.Info, info));
        }

        public void AddWarning(string warning)
        {
            this.m_Logs.Add(new PaLogEntry(PaLogType.Warning, warning));
        }

        public void EndJob(string job)
        {
            this.m_Logs.Add(new PaLogEntry(PaLogType.EndJob, job));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetLogsTable formats log entries in an HtmlTable
        /// </summary>
        /// <history>
        /// [jbrinkman] 24/11/2004  Created new method.  Moved from WebUpload.ascx.vb
        /// </history>
        /// -----------------------------------------------------------------------------
        public HtmlTable GetLogsTable()
        {
            IEnumerator refObjectHelperL0=null;
            HtmlTable arrayTable = new HtmlTable();
            try
            {
                refObjectHelperL0 = this.Logs.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    PaLogEntry LogEntry = (PaLogEntry) refObjectHelperL0.Current;
                    HtmlTableRow tr = new HtmlTableRow();
                    HtmlTableCell tdType = new HtmlTableCell();
                    tdType.InnerText = DotNetNuke.Services.Localization.Localization.GetString("LOG.PALogger." + LogEntry.Type.ToString());
                    HtmlTableCell tdDescription = new HtmlTableCell();
                    tdDescription.InnerText = LogEntry.Description;
                    tr.Cells.Add(tdType);
                    tr.Cells.Add(tdDescription);
                    switch (LogEntry.Type)
                    {
                        case PaLogType.Info:
                            tr.Attributes.Add("class", this.NormalClass);
                            break;

                        case PaLogType.Warning:
                        case PaLogType.Failure:
                            tr.Attributes.Add("class", this.ErrorClass);
                            break;

                        case PaLogType.StartJob:
                        case PaLogType.EndJob:
                            tr.Attributes.Add("class", this.HighlightClass);
                            break;
                    }
                    arrayTable.Rows.Add(tr);
                    if (LogEntry.Type == PaLogType.EndJob)
                    {
                        HtmlTableRow SpaceTR = new HtmlTableRow();
                        HtmlTableCell SpaceTD = new HtmlTableCell();
                        SpaceTD.ColSpan = 2;
                        SpaceTD.InnerHtml = "&nbsp;";
                        SpaceTR.Cells.Add(SpaceTD);
                        arrayTable.Rows.Add(SpaceTR);
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return arrayTable;
        }

        public void StartJob(string job)
        {
            this.m_Logs.Add(new PaLogEntry(PaLogType.StartJob, job));
        }

        public string ErrorClass
        {
            get
            {
                if (this._ErrorClass == "")
                {
                    this._ErrorClass = "NormalRed";
                }
                return this._ErrorClass;
            }
            set
            {
                this._ErrorClass = value;
            }
        }

        public string HighlightClass
        {
            get
            {
                if (this._HighlightClass == "")
                {
                    this._HighlightClass = "NormalBold";
                }
                return this._HighlightClass;
            }
            set
            {
                this._HighlightClass = value;
            }
        }

        public ArrayList Logs
        {
            get
            {
                return this.m_Logs;
            }
        }

        public string NormalClass
        {
            get
            {
                if (this._NormalClass == "")
                {
                    this._NormalClass = "Normal";
                }
                return this._NormalClass;
            }
            set
            {
                this._NormalClass = value;
            }
        }

        public bool Valid
        {
            get
            {
                return this.m_Valid;
            }
        }
    }
}

